#ifndef FAST_PWM_H
#define FAST_PWM_H

void timer1PWMInit ()
{
  // Timer/Counter 1 initialization
  // Clock source: System Clock
  // Clock value: 1000.000 kHz
  // Mode: Fast PWM top=03FFh
  // OC1A output: Non-Inv.
  // OC1B output: Discon.
  // Noise Canceler: Off
  // Input Capture on Falling Edge
  // Timer 1 Overflow Interrupt: Off
  // Input Capture Interrupt: Off
  // Compare A Match Interrupt: Off
  // Compare B Match Interrupt: Off
  TCCR1A=0x03;//Fast PWM top=03FFh
  TCCR1B=0x0A;//1000.000 kHz
  TCNT1H=0x00;
  TCNT1L=0x00;
  ICR1H=0x00;
  ICR1L=0x00;
  OCR1A=0x0000;
  OCR1B=0x0000;
  
  PORTB|=0x00;
  DDRB|=0x06;
}

void timer1PWMASet(unsigned int pwmDuty)
{
  // set PWM (output compare) duty for channel A
  // this PWM output is generated on OC1A pin
  // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
  //          pwmDuty should be in the range 0-511 for 9bit PWM
  //          pwmDuty should be in the range 0-1023 for 10bit PWM
  //outp( (pwmDuty>>8), OCR1AH);      // set the high 8bits of OCR1A
  //outp( (pwmDuty&0x00FF), OCR1AL);  // set the low 8bits of OCR1A
  OCR1A = pwmDuty;
}

void timer1PWMAOn(void)
{
  // turn on channel A (OC1A) PWM output
  // set OC1A as non-inverted PWM
  TCCR1A|=(1<<COM1A1)|(1<<COM1A0);
}

void timer1PWMAOff(void)
{
  // turn off channel A (OC1A) PWM output
  // set OC1A (OutputCompare action) to none
  TCCR1A&=~((1<<COM1A1)|(1<<COM1A0));
}

// For channel B
void timer1PWMBSet(unsigned int pwmDuty)
{
  OCR1B = pwmDuty;
}

void timer1PWMBOn(void)
{
  TCCR1A|=(1<<COM1B1)|(1<<COM1B0);
}

void timer1PWMBOff(void)
{
  TCCR1A&=~((1<<COM1B1)|(1<<COM1B0));
}

#endif